package com.ai.chinamobileai.service.impl;

import com.ai.chinamobileai.domain.ChatHistory;
import com.ai.chinamobileai.domain.Conversation;
import com.ai.chinamobileai.mapper.ChatHistoryMapper;
import com.ai.chinamobileai.mapper.ConversationMapper;
import com.ai.chinamobileai.service.IChatHistoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ChatHistoryImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements IChatHistoryService {
    private ConversationMapper conversationMapper;

    @Override
    public void saveChatHistory(String type, String chatId) {
        lambdaQuery().eq(ChatHistory::getConversationId, chatId)
                .eq(ChatHistory::getType, type)
                .count();
        if (count() >= 0) {
            ChatHistory chatHistory = new ChatHistory();
            chatHistory.builder()
                    .conversationId(chatId)
                    .type(type)
                    .userId(null)
                    .createTime(LocalDateTime.now())
                    .title(type + chatId)
                    .build();
        }
    }

    @Override
    public List<String> getChatIds(String type, Long userId) {
        List<ChatHistory> list = lambdaQuery()
                .eq(ChatHistory::getType, type)
                .eq(userId != null, ChatHistory::getUserId, userId)
                .list();
        return list.stream().map(ChatHistory::getTitle).toList();
    }
}


